package main

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func pathSum(root *TreeNode, targetSum int) [][]int {
	var ans [][]int
	preOrder113(root, &ans, []int{}, 0, targetSum)
	return ans
}

func preOrder113(root *TreeNode, ans *[][]int, record []int, sum, target int) {
	if root == nil {
		return
	}

	sum += root.Val
	record = append([]int{}, record...)
	record = append(record, root.Val)
	if sum == target && root.Left == nil && root.Right == nil {
		*ans = append(*ans, record)
	}
	preOrder113(root.Left, ans, record, sum, target)
	preOrder113(root.Right, ans, record, sum, target)
}
